//
//  DatePickView.m
//  buttton
//
//  Created by 邹吕 on 2017/3/20.
//  Copyright © 2017年 邹吕. All rights reserved.
//

#import "DatePickView.h"

#define kDatePickerViewHeight 260
#define kTopViewHeight 40

#define kScreenWidth        [UIScreen mainScreen].bounds.size.width

@interface DatePickView (){
    UIView        *_wholeView;
    UIView        *_topView;
}

@property (nonatomic) UIDatePicker *datepicker;

@property (nonatomic) NSString *currentDateStr;
@property (nonatomic) NSString *tipsStr;

@property (nonatomic) NSString *dateStr;

@property (nonatomic) BOOL isShowMax;
@property (nonatomic) BOOL isForward;
@property (nonatomic) BOOL isDisplaytime;  // 显示时分
@end

@implementation DatePickView

- (id)initWithFrame:(CGRect)frame withDateStr:(NSString *)dateStr withIsMaxDate:(BOOL)showMax withNoForward:(BOOL)isForward WithTips:(NSString *)tipsStr{
    
    self = [super initWithFrame:frame];
    if (self)
    {
        self.userInteractionEnabled = YES;
        
        _dateStr = dateStr;
        _isShowMax = showMax;
//        _isForward = isForward;
        self.tipsStr = tipsStr;
        self.backgroundColor = [UIColor colorWithRed:10.f/255 green:10.f/255 blue:10.f/255 alpha:0.6];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenBottomView)];
        [self addGestureRecognizer:tap];
    
        [self createView];
        
    }
    return self;
}
- (id)initWithFrame:(CGRect)frame withDateStr:(NSString *)dateStr withIsMaxDate:(BOOL)showMax withNoForward:(BOOL)isForward withIsDisplaytime:(BOOL)isDisplaytime{
    self = [super initWithFrame:frame];
       if (self)
       {
           self.userInteractionEnabled = YES;
           
           _dateStr = dateStr;
           _isShowMax = showMax;
           _isForward = isForward;
           _isDisplaytime = isDisplaytime;
           self.backgroundColor = [UIColor colorWithRed:10.f/255 green:10.f/255 blue:10.f/255 alpha:0.6];
           
           UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenBottomView)];
           [self addGestureRecognizer:tap];
       
           [self createView];
           
       }
       return self;
}
-(void)createView{
    
    // 弹出的整个视图
    _wholeView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kDatePickerViewHeight)];
    NSLog(@"%@",@(kDatePickerViewHeight));
    _wholeView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_wholeView];
    
    // 头部按钮视图
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
    line.backgroundColor = [UIColor lightGrayColor];
    [_wholeView addSubview:line];
    
    _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0.5, kScreenWidth, kTopViewHeight)];
    _topView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0];;
    [_wholeView addSubview:_topView];
    
    // 防止点击事件触发
    UITapGestureRecognizer *topTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
    [_topView addGestureRecognizer:topTap];
    
    NSArray *buttonTitleArray = @[@"取消",@"确定"];
    for (int i = 0; i <buttonTitleArray.count ; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i*(kScreenWidth-50), 0, 50, kTopViewHeight);
        [button setTitleColor:[UIColor colorWithRed:0/255.0 green:122/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
        [button setTitle:buttonTitleArray[i] forState:UIControlStateNormal];
        [_topView addSubview:button];
        
        button.tag = i;
        [button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    // datepicker
    self.datepicker = [[UIDatePicker alloc]init];
    [_wholeView addSubview:self.datepicker];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, _topView.width-100, _topView.height)];
    label.textColor = [UIColor colorWithHexString:@"#AAAAAA"];
    label.text = StringIsEmpty(self.tipsStr)?@"请选择":self.tipsStr;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:14];
    [_topView addSubview:label];
    
    
    
    if (@available(iOS 13.4, *)) {
        self.datepicker.preferredDatePickerStyle = UIDatePickerStyleWheels ;
    } else {
        // Fallback on earlier versions
    }
    
    //设置时区
    [self.datepicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];
    [self.datepicker setTimeZone:[NSTimeZone localTimeZone]];
    
    //设置日期模式
    if (self.isDisplaytime == YES) {
        self.datepicker.datePickerMode = UIDatePickerModeDateAndTime;
    } else {
        self.datepicker.datePickerMode = UIDatePickerModeDate;
    }
    self.datepicker.frame = CGRectMake(0, kTopViewHeight, kScreenWidth, kDatePickerViewHeight - kTopViewHeight);
    //设置时间可选择的范围
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *currentDate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    
    
//    if (_isForward == YES) {
//        [comps setYear:-0];//设置最小时间为：当前时间
//    }else{
//        [comps setYear:-5];//设置最小时间为：当前时间前推5年
//    }
//    NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
     
    NSDate *maxDate = nil;
    //设置最大时间为当前时间
    if (_isShowMax == YES) {
        [comps setYear:99];
        maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
    }
    else{
        maxDate = currentDate;
    }
    
    [self.datepicker setMaximumDate:maxDate];
//    [self.datepicker setMinimumDate:minDate];
   
    //设置当前显示时间
  //  [self.datepicker setDate:[NSDate date] animated:YES];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    if (self.isDisplaytime == YES) {
        [formatter setDateFormat:@"yyyy-MM-dd HH"];
        self.datepicker.calendar = [NSCalendar currentCalendar];
        self.datepicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_GB"];
    } else {
        [formatter setDateFormat:@"yyyy-MM-dd"];//
    }
    
    if (StringIsEmpty(_dateStr)) {
        NSString *time = [formatter stringFromDate:[NSDate date]];
        NSDate *resDate = [formatter dateFromString:time];
        [self.datepicker setDate:resDate animated:YES];
    } else {
        NSDate *resDate = [formatter dateFromString:_dateStr];
        [self.datepicker setDate:resDate animated:YES];
    }
     
    
    
    
   
    //监听值的改变
    [self.datepicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];
    
  
    
    
}

-(void)changeDate:(UIDatePicker*)datePicker{
    
    NSLog(@"date ==== %@",datePicker.date);
    
    NSString *str = [NSString stringWithFormat:@"%@",datePicker.date];
    self.currentDateStr = str;

}

- (void)buttonEvent:(UIButton *)button{
    
//    if (button.tag == 1) {
//        if (self.delegate && [self.delegate respondsToSelector:@selector(sureCurrentDate:)]) {
//            [self.delegate sureCurrentDate:self.currentDateStr];
//        }
//        [self hiddenBottomView];
//        
//    }else{
//        [self hiddenBottomView];
//    }
    
    
    if (button.tag == 1) {
        
        //当前时间转字符串
        
        NSDate *date = [self.datepicker date];
        
        NSDateFormatter *dateFor = [[NSDateFormatter alloc]init];
        
        if (self.isDisplaytime == YES) {
               dateFor.dateFormat = @"yyyy-MM-dd HH";
           } else {
               dateFor.dateFormat = @"yyyy-MM-dd";
           }
        
        
        
        NSString *dateStr = [dateFor stringFromDate:date];
        
        self.currentDateStr = dateStr;
        
        if (_block) {
            _block(self.currentDateStr);
        }
        
        [self hiddenBottomView];
        
    }else{
        
        [self hiddenBottomView];
    }

    
    
}

- (void)showBoomView{
    [UIView animateWithDuration:0.3 animations:^
     {
         self->_wholeView.frame = CGRectMake(0, kScreenHeight-kDatePickerViewHeight, kScreenWidth, kDatePickerViewHeight);
         
     } completion:^(BOOL finished) {}];
    
    
}

-(void)hiddenBottomView{
   
    [UIView animateWithDuration:0.3 animations:^
     {
         self->_wholeView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kDatePickerViewHeight);
         
     } completion:^(BOOL finished) {
         [self removeFromSuperview];
         
     }];

}

@end
